//
//  DetailNewProductTableViewCell.m
//  Beauty
//
//  Created by Snow on 16/9/10.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "DetailNewProductTableViewCell.h"
#import "GXMacro.h"
#import "DetailNewProductCollectionViewCell.h"
@interface DetailNewProductTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@end
@implementation DetailNewProductTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _layout.minimumLineSpacing = 5;
        _layout.minimumInteritemSpacing = 5;
        _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[DetailNewProductCollectionViewCell class] forCellWithReuseIdentifier:@"DetailNewProductCollectionViewCell"];
        [self.contentView addSubview:_collectionView];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake(100, self.contentView.bounds.size.height - 10);
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left);
        make.right.mas_equalTo(self.contentView.mas_right);
        make.top.mas_equalTo(self.contentView.mas_top);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
    }];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailNewProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailNewProductCollectionViewCell" forIndexPath:indexPath];
    if (_array.count != 0) {
        NSDictionary *dic = [_array objectAtIndex:indexPath.row];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:dic[@"goods_image_url"]]];
        cell.labelOfProce.text =[NSString stringWithFormat:@"¥ %@", dic[@"goods_price"]];
    }
    return cell;
}
- (void)setArray:(NSArray *)array
{
    _array = array;
    [_collectionView reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(getGoodDetailWithGoodId:)]) {
        NSString *string = [_array objectAtIndex:indexPath.row][@"goods_id"];
        [self.delegate getGoodDetailWithGoodId:string];
    }
}
@end
